package cn.com.sbell.vss.commander;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import cn.com.sbell.vss.ICommander;
import cn.com.sbell.vss.Response;
import cn.com.sbell.vss.util.XmlUtil;

public class ShowVersion implements ICommander {
	private static final Log log = LogFactory.getLog(ShowVersion.class);

	public Response execute(HttpServletRequest request) {
		if(log.isDebugEnabled()){
		log.debug("Show Version ...");
		}
		Response response = Response.createResponse(request);
		Document doc = DocumentHelper.createDocument();
		Element root = doc.addElement("parameters");

		Properties props = new Properties();
		try {
			InputStream in = ShowVersion.class
					.getResourceAsStream("/version.properties");
			props.load(in);
		} catch (IOException ex) {
			if(log.isWarnEnabled()){
			log.warn("Get version.properties failed", ex);
			}
		}
		Enumeration en = props.propertyNames();
		if (en != null) {
			while (en.hasMoreElements()) {
				String name = (String) en.nextElement();
				String value = props.getProperty(name);
				XmlUtil.addElement(root, name, value);
			}
		}

		response.setParameters(doc.asXML());
		return response;
	}
}
